<?php 
	include ('lib/twitese.php');
	$title = "消息查看";
	include ('inc/header.php');
	
	if (!isLogin()) header('location: login.php');
	
	$t = getTwitter();
	if ( isset($_GET['id']) ) {
		$statusid = $_GET['id'];
		$status = $t->showStatus($statusid);
		
		$p = 1;
		if (isset($_GET['p'])) {
			$p = (int) $_GET['p'];
			if ($p <= 0) $p = 1;
		}
		
		$rid = 0;
		$reply_text = "";
		if (isset($_GET['rid'])) {
			$rid = (int) $_GET['rid'];
		}
		if (isset($_GET['rtext'])) {
			$reply_text = "回复@" . $_GET['rtext'] . " ";
		}
		
		$comments = $t->comments($statusid, $p);
		$count = $t->commentCount($statusid);
		$count = $count[0]->comments;
		
		if (!$status) {
			header('location: error.php');
		}
		$user = $status->user;
		$date = formatDate($status->created_at);
		$text = formatText($status->text);
		
		$is_rt = isset($status->retweeted_status);
		
		$status_id = $status->id;
		$user = $status->user;
		$date = formatDate($status->created_at);
		$text = formatText($status->text);
		
		if ($is_rt) {
			$rttext = formatText($status->retweeted_status->text);
			$rtuser = $status->retweeted_status->user;
			$rtid = $status->retweeted_status->id;
		}
		
		
		
	
		
	} else {
		header('location: error.php');
	}
	
	
?>
<script type="text/javascript">
	$(function(){
		$(".rt_btn").live("click", function(e){
			e.preventDefault();
			onRT($(this));
		});
		
		$(".favor_btn").live("click", function(e){
			e.preventDefault();
			onFavor($(this));
		});
	
		$(".comment_btn").live("click", function(e){
			e.preventDefault();
			onComment($(this));
		});
		$(".comment_delete_btn").live("click", function(e){
			e.preventDefault();
			onCommentDelete($(this));
		});
		
		$(".s_comment_reply").hide();			
		$(".s_comment_ctn").hover(function(){
			$(this).find(".s_comment_reply").show();
		}, function(){
			$(this).find(".s_comment_reply").hide();
		})
	});
</script>
<style type="text/css" media="screen">
.timeline li:hover{background:#ffffff}
</style>
<div id="statuses">
	<div class="weibo_ptWrapPdd">
		<?php 		
			if ( isset($_POST['comment_input']) && isset($_POST['comment_reply_id']) ) {
				
				if (!testReferer()) exit();
				if (trim($_POST['comment_input']) == '') {
					echo "<div id=\"sentTip\">评论失败，内容不能为空</div>";
				} else {
					$result = $t->comment($_POST['comment_input'], $statusid, $_POST['comment_reply_id']);
					if ( isset($_POST['sent_status']) && $_POST['sent_status'] == "on" ) {
						$t->repost($_POST['comment_input'], $statusid);
					}
					if ($result) {
						echo "<div id=\"sentTip\">评论成功</div>";
					} else {
						echo "<div id=\"sentTip\">评论失败，请重试</div>";
					}
				}
			}
		?>
		<h2><?php echo $user->screen_name ?>的消息</h2>
		<div class="clear"></div>
	</div>
	<div class="weibo_postList">
		<ol class="timeline">
			<li>
				<span class="status_author">
					<a href="user.php?id=<?php echo $user->screen_name ?>" target="_blank"><img src="<?php echo $user->profile_image_url ?>" /></a>
				</span>
				<span class="status_body">
					<span class="status_id"><?php echo $statusid ?></span>
					<span class="status_word"><a class="user_name" href="user.php?id=<?php echo $user->screen_name ?>"><?php echo $user->screen_name ?></a> <?php echo $text ?></span>
				<?php
					if ($is_rt) {
				?>
					<div class="inline_rt">
						<span class="status_body inline_rt_body">
							<input type="hidden" class="rt_id" value="<?php echo $rtid ?>" />
							<span class="status_word"><a class="user_name" href="user.php?id=<?php echo $rtuser->screen_name ?>" target="_blank">@<?php echo $rtuser->screen_name?></a>: <?php echo $rttext  ?></span>
							
						</span>
						<div class="clear"></div>
					</div>
				<?php } ?>
					<span class="status_info">
						<a class="rt_btn" href="a_rt.php?id=<?php echo $status->id?>">转发</a>
						<a class="favor_btn" href="a_favor.php?id=<?php echo $status->id?>">收藏</a>
						<?php if ($status->in_reply_to_status_id) {?><span class="in_reply_to"> <a href="status.php?id=<?php echo $status->in_reply_to_status_id ?>">对 <?php echo $status->in_reply_to_screen_name?> 的回复</a></span> <?php }?>
						<span class="source">通过<?php echo $status->source ?></span>
						<span class="date"><a href="https://twitter.com/<?php echo $user->screen_name ?>/status/<?php echo $statusid ?>" target="_blank"><?php echo $date ?></a></span>
					</span>
				</span>
				<span class="s_comment_hd">评论(<?php echo $count ?>)</span>
				<div class="s_comment_form">
					<form method="post" action="status.php?id=<?php echo $statusid ?>">
						<textarea name="comment_input" class="s_comment_input"><?php echo $reply_text ?></textarea>
						<span class="s_comment_cb"><input type="checkbox" class="comment_status" name="sent_status" id="sent_status" /> <label for="sent_status">同时发送一条消息</label></span>
						<input type="submit" name="comment_submit"  class="s_comment_submit" value="评论"/>
						<input type="hidden" name="comment_reply_id"  class="s_comment_reply_id" value="<?php echo $rid ?>" />
					</form>
				</div>
				<div class="clear"></div>
				<?php
					if (count($comments) == 0) {
						echo '<div class="s_comment_empty">无评论</div>';
					}
					echo '<div class="s_comment_warp">';
					foreach ($comments as $comment) {
						$user = $comment->user;
						$text = formatText($comment->text);
						$date = formatDate($comment->created_at);
						$commentid = $comment->id;
						echo "
						<div class=\"s_comment_ctn\">
							<span class=\"s_comment_author\">
								<a href=\"user.php?id=$user->screen_name\" target=\"_blank\"><img src=\"$user->profile_image_url\" title=\"$user->screen_name\" /></a>
							</span>
							<span class=\"s_comment_body\">
								<span class=\"status_word\">
									<input type=\"hidden\" class=\"comment_id\" value=\"$commentid\" />
									<a class=\"user_name\" href=\"user.php?id=$user->screen_name\" target=\"_blank\">$user->screen_name</a> $text 
									<span class=\"s_comment_date\">{$date}</span>
						";
						if ($user->screen_name == getEncryptCookie('twitese_name')) {
							echo "	<a href=\"a_del.php?id=$commentid&t=c\" class=\"s_comment_delete_btn\">删除</a>";
						} else {
							echo "	<a href=\"status.php?id=$statusid&rid=$commentid&rtext=$user->screen_name\" class=\"s_comment_reply\">回复</a>";
						}
						
						echo "	</span>
							</span>
							<div class=\"clear\"></div>
						</div>
						";
					}
					echo '</div>';
					
					$output = "<div id=\"pagination\">";
					
					if ($p >1) $output .= "<a href=\"status.php?id=$statusid&p=" . ($p-1) . "\">上一页</a>";
					if (count($comments) != 0) $output .= "<a href=\"status.php?id=$statusid&p=" . ($p+1) . "\">下一页</a>";
					
					$output .= "</div>";
					
					echo $output;
				?>
				
			</li>
		</ol>
	</div>
</div>

<?php 
	include ('inc/sidebar.php');
?>

<?php 
	include ('inc/footer.php');
?>
